package com.beaven.imclient.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.beaven.imclient.R;
import com.beaven.imclient.utils.ImageUtil;
import com.beaven.model.data.helper.MessageHelper;
import com.beaven.model.model.db.Message;
import com.beaven.model.model.db.User;
import com.beaven.model.model.sp.Account;
import com.beaven.model.utils.DataUtil;
import com.bumptech.glide.Glide;
import de.hdodenhof.circleimageview.CircleImageView;
import java.util.ArrayList;
import java.util.List;
import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;
import timber.log.Timber;

/**
 * author: Beaven
 * time: 2017/6/1 9:43
 *
 * 聊天Adapter
 */

public class ChatUserAdapter extends RecyclerView.Adapter<ChatUserAdapter.BaseChatHolder> {

  private List<Message> messageList = new ArrayList<>();

  private static int[] drawables = {
      R.drawable.shape_chat_bg_one, R.drawable.shape_chat_bg_two, R.drawable.shape_chat_bg_three,
      R.drawable.shape_chat_bg_four
  };

  private Context context;

  public ChatUserAdapter(Context context) {
    this.context = context;
  }

  public List<Message> getMessageList() {
    return messageList;
  }

  @Override public BaseChatHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View root = LayoutInflater.from(parent.getContext()).inflate(viewType, parent, false);
    return new BaseChatHolder(root);
  }

  @Override public void onBindViewHolder(final BaseChatHolder holder, int position) {
    Message message = messageList.get(position);
    message.load();
    boolean isRight = DataUtil.equals(message.getSender().getId(), Account.getUserId());
    holder.text_content.setText(message.getContent());
    holder.image_status_error.setVisibility(
        message.getStatus() == Message.STATUS_FAILED ? View.VISIBLE : View.GONE);
    if (isRight) {
      holder.text_content.setBackgroundResource(R.drawable.shape_chat_bg_right);
      ImageUtil.show(Glide.with(context), holder.image_account_chat, Account.getAvatar());
    } else {
      Observable.just(message.getId()).subscribeOn(Schedulers.io()).map(new Func1<String, User>() {
        @Override public User call(String s) {
          return MessageHelper.findSenderId(s);
        }
      }).observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<User>() {
        @Override public void onCompleted() {

        }

        @Override public void onError(Throwable e) {
          Timber.e(e);
        }

        @Override public void onNext(User user) {
          if (user != null) {
            ImageUtil.show(Glide.with(context), holder.image_account_chat, user.getAccount());
            int chatColor = user.getChatColor();
            int color = chatColor >= 0 && chatColor < drawables.length ? chatColor : 1;
            holder.text_content.setBackgroundResource(drawables[color]);
          } else {
            holder.image_account_chat.setImageResource(R.drawable.account);
            holder.text_content.setBackgroundResource(R.drawable.shape_chat_bg_three);
          }
        }
      });
    }
  }

  @Override public int getItemViewType(int position) {
    Message message = messageList.get(position);
    message.load();
    boolean isRight = DataUtil.equals(message.getSender().getId(), Account.getUserId());
    if (message.getType() == Message.TYPE_STR) {
      return isRight ? R.layout.item_chat_user_right : R.layout.item_chat_user_left;
    }
    return super.getItemViewType(position);
  }

  @Override public int getItemCount() {
    return messageList == null ? 0 : messageList.size();
  }

  class BaseChatHolder extends RecyclerView.ViewHolder {
    TextView text_content;
    CircleImageView image_account_chat;
    ImageView image_status_error;

    BaseChatHolder(View itemView) {
      super(itemView);
      text_content = (TextView) itemView.findViewById(R.id.text_msg);
      image_account_chat = (CircleImageView) itemView.findViewById(R.id.image_account_chat);
      image_status_error = (ImageView) itemView.findViewById(R.id.image_status_error);
    }
  }
}
